#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> pii;
const int N = 1e5 + 10, mod = 1e9 + 7;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
template <typename T>
class ST
{
    using VT = vector<T>;
    using VVT = vector<VT>;
    using func_type = function<T(const T &, const T &)>;
    VVT ST1;
    static T default_func(const T &t1, const T &t2) { return max(t1, t2); }
    func_type op;

public:
    ST(const vector<T> &v, func_type _func = default_func)
    {
        op = _func;
        int len = v.size(), l1 = ceil(log2(len)) + 1;
        ST1.assign(len, VT(l1, 0));
        for (int i = 0; i < len; ++i)
        {
            ST1[i][0] = v[i];
        }
        for (int j = 1; j < l1; ++j)
        {
            int pj = (1 << (j - 1));
            for (int i = 0; i + pj < len; ++i)
            {
                ST1[i][j] = op(ST1[i][j - 1], ST1[i + (1 << (j - 1))][j - 1]);
            }
        }
    }

    T query(int l, int r)
    {
        int lt = r - l + 1;
        int q = floor(log2(lt));
        return op(ST1[l][q], ST1[r - (1 << q) + 1][q]);
    }
};
// 下标0~n-1
template <typename T>
T min_func(const T &t1, const T &t2)
{
    return min(t1, t2);
}
void solve()
{
    int n, m;
    cin >> n >> m;
    vector<int> a(n, 0);
    for (int i = 0; i < n; i++)
        cin >> a[i];
    ST<int> st(a);
    ST<int> stmin(a, min_func<int>);
    while (m--)
    {
        int l, r;
        cin >> l >> r;
        l--, r--;
        cout << st.query(l, r) << "\n";
    }
}

signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    // cin >> t;
    while (t--)
        solve();
    return 0;
}
